int *path;
int **result;
int pathTop;
int resultTop;
int sum;
int *resultColumnSizes; // 存放每个结果的长度

void backtracking(int *candidates, int target, int startIndex, int candidatesSize) {
    if (sum > target) return;
    if (sum == target) {
        int *temp = (int *)malloc(sizeof(int) * pathTop);
        for(int i = 0; i < pathTop; i++) { // 1# 循环条件写成 i < candidatesSize
            temp[i] = path[i];
        }
        resultColumnSizes[resultTop] = pathTop;
        result[resultTop++] = temp;
        return;
    }

    for(int i = startIndex; i < candidatesSize; i++) {
        path[pathTop++] = candidates[i];
        sum += path[pathTop - 1]; // 2# 忘记写求和
        backtracking(candidates, target, i, candidatesSize);
        sum -= path[--pathTop];
    }
}

int** combinationSum(int* candidates, int candidatesSize, int target, int* returnSize, int** returnColumnSizes) {
    path = (int *)malloc(sizeof(int) * 1000);
    result = (int **)malloc(sizeof(int *) * 1000);
    pathTop = resultTop = sum = 0;
    resultColumnSizes = (int *)malloc(sizeof(int) * 1000); // 3# 忘记分配内存
    backtracking(candidates, target, 0, candidatesSize);

    *returnSize = resultTop;
    *returnColumnSizes = (int *)malloc(sizeof(int) * resultTop);  // 4# 忘记分配内存
    for(int i = 0; i < resultTop; i++) {
        (*returnColumnSizes)[i] = resultColumnSizes[i];
    }
    return result;
} 